package crawler;

import parser.*;

import java.util.List;
import java.util.LinkedHashMap;
import java.util.ArrayList;
import java.util.HashMap;

import java.io.InputStream;

/**
 *
 * @author Ilio Catallo, Eleonora Ciceri
 * @version: 1.0
 * 
 * Description:  The class implements a google image crawler
 * 
 * Date: Nov 17, 2011
 */
public class GooglePhotoCrawler extends PhotoCrawler {

    public GooglePhotoCrawler(LinkedHashMap<String, String> query, String path) {
        
        super("https", query, path);
        this.parser = new JSONParser();
        this.isQueryToKeep = Boolean.FALSE;
        
        this.attributes.put("url", null);
   
        this.navigationPath.add("responseData");
        this.navigationPath.add("results");        
    }
    
    @Override
    protected List<String> composeURLs(List<HashMap<String, String>> responseAttributes) {
        // Variables
        List<String> URLs = new ArrayList<String>();
        
        for (HashMap<String, String> currAttributes: responseAttributes)
            URLs.add((String) currAttributes.get("url"));
       
        return URLs;
    }

    @Override
    protected List<HashMap<String, String>> findIncrAttributes() {
       // Variables
        HashMap<String, String> attributes = new HashMap<String, String>();
        List<String> navigationPath = new ArrayList<String>();
        
        // Prepare the navigation path and the attributes to look for
        navigationPath.add("responseData");
        navigationPath.add("cursor");
        navigationPath.add("pages");
        attributes.put("label", null);
        attributes.put("start", null);
        
        // Execute the query
        this.restClient.executeQuery(this.query);
        InputStream responseContent = restClient.getResponseContent();
        
        // Parse the response
        this.parser.setNavigationPath(navigationPath);
        this.parser.setAttributes(attributes);
        this.parser.parse(responseContent);
        
        // Return the increment attributes
        List<HashMap<String, String>> incrementAttributes = new ArrayList<HashMap<String, String>>();
        incrementAttributes.addAll(this.parser.getResponseAttributes());
        
        return incrementAttributes;
        
    }
    
    
}
